/**
 * 用户相关API服务
 */
import api from './index';
import { ApiResponse, User, Contact, Coupon } from './types';

/**
 * 用户登录
 */
export interface LoginParams {
  username: string;
  password: string;
}

export interface LoginResult {
  token: string;
  user: User;
}

export const login = (params: LoginParams): Promise<ApiResponse<LoginResult>> => {
  return api.post('/user/login', params);
};

/**
 * 用户注册
 */
export interface RegisterParams {
  username: string;
  password: string;
  phone: string;
  email?: string;
}

export const register = (params: RegisterParams): Promise<ApiResponse<{ userId: string }>> => {
  return api.post('/user/register', params);
};

/**
 * 获取用户信息
 */
export const getUserInfo = (): Promise<ApiResponse<User>> => {
  return api.get('/user/info');
};

/**
 * 更新用户信息
 */
export const updateUserInfo = (params: Partial<User>): Promise<ApiResponse<null>> => {
  return api.put('/user/info', params);
};

/**
 * 获取常用联系人列表
 */
export const getContactList = (): Promise<ApiResponse<Contact[]>> => {
  return api.get('/user/contacts');
};

/**
 * 添加常用联系人
 */
export const addContact = (contact: Omit<Contact, 'id'>): Promise<ApiResponse<{ contactId: string }>> => {
  return api.post('/user/contacts', contact);
};

/**
 * 更新常用联系人
 */
export const updateContact = (contactId: string, contact: Partial<Omit<Contact, 'id'>>): Promise<ApiResponse<null>> => {
  return api.put(`/user/contacts/${contactId}`, contact);
};

/**
 * 删除常用联系人
 */
export const deleteContact = (contactId: string): Promise<ApiResponse<null>> => {
  return api.delete(`/user/contacts/${contactId}`);
};

/**
 * 获取用户优惠券列表
 */
export const getCouponList = (status?: 'all' | 'unused' | 'used' | 'expired'): Promise<ApiResponse<Coupon[]>> => {
  return api.get('/user/coupons', { params: { status } });
};

/**
 * 收藏/取消收藏酒店
 */
export const toggleFavoriteHotel = (hotelId: string, isFavorite: boolean): Promise<ApiResponse<null>> => {
  return api.post('/user/favorites', { hotelId, isFavorite });
};

/**
 * 获取收藏的酒店列表
 */
export const getFavoriteHotels = (page = 1, pageSize = 10): Promise<ApiResponse<any>> => {
  return api.get('/user/favorites', { params: { page, pageSize } });
}; 